﻿@inject IIssueService IssueService
@inject IUserService UserService
@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthProvider
@inject IJSRuntime JsRuntime

<Card IsShadow="true" Color="@(IssueDto.IsClosed? Color.Success:Color.Danger)">
    <BodyTemplate>
        @if (IssueDto.IsClosed)
        {
            @* <hr size="1" style="border-top:2px green solid;"> *@
            <span style="font-weight: bold;color:green ">@IssueDto.Title</span>
            <span class="text-success">(异常已关闭)</span>
        }
        else
        {
            @* <hr size="1" style="border-top:2px red solid;"> *@
            <CompUpdateIcon Roles="" Url="@($"issue/update/{IssueDto.Id}")" />
            <span style="font-weight: bold;color:red ">@IssueDto.Title</span>
            <span class="text-danger">(异常未关闭)</span>
        }
        <br />

        <span style="color: dodgerblue">上报人员：@(IssueDto.Reporter)  /</span>
        <span style="color: dodgerblue">上报日期：@IssueDto.CreationTime.ToString("MM/dd")</span>
        <br />

        <span style="color: dodgerblue">异常内容：</span>
        <CompMultiLineText Text="@IssueDto.Content" />

        <span style="color: dodgerblue">异常图片：</span>
        <br />
        <CompMultiImgs ContentUrl="@IssueDto.ContentUrl" />
        <br />

        @if (IssueDto.IsClosed)
        {
            <span style="color: dodgerblue">关闭人员：@(IssueDto.Responder)  /</span>
            <span style="color: dodgerblue">
                关闭日期：@(IssueDto.CloseTime != null ?
               IssueDto.CloseTime.Value.ToString("MM/dd") : "")
            </span>
            <br />


            <span style="color: dodgerblue">措施内容：</span>
            <CompMultiLineText Text="@IssueDto.Solution" />

            <span style="color: dodgerblue">措施图片：</span>
            <br />
            <CompMultiImgs ContentUrl="@IssueDto.SolutionUrl" />
            <br />
            <button class="btn btn-outline-success mb-2" @onclick="OpenIssue">重新打开</button>
        }
        else
        {
            <div class="input-group mb-2">
                <span class="input-group-text">措施内容</span>
                <textarea class="form-control" placeholder="请详细描解决方案，可输入多行" style="height: 180px" @bind="@IssueDto.Solution"></textarea>
            </div>

            <ImagesUpload Title="措施图片(可添加多个)" @bind-MultiImageUrl="@IssueDto.SolutionUrl" />
            <br />
            <button class="btn btn-outline-success mb-2 me-2" @onclick="SaveSolution">保存措施</button>
            <button class="btn btn-outline-danger mb-2" @onclick="CloseIssue">关闭异常</button>
        }
    </BodyTemplate>
</Card>






@code {
    [Parameter]
    public IssueDto? IssueDto { get; set; }
    protected override async Task OnInitializedAsync()
    {

    }

    private async void OpenIssue()
    {
        IssueDto.CloseTime=null;
        IssueDto.IsClosed = false;
        IssueDto.ResponderId = null;
        IssueDto.Responder = string.Empty;

        var result = await IssueService.UpdateSolutionAsync(IssueDto.Id.Value, IssueDto);
        //todo:弹出提示
        if (result.IsSuccessStatusCode)
        {
            await JsRuntime.InvokeVoidAsync("alert", "已重新打开异常！");
        }
        StateHasChanged();
    }

    private async void SaveSolution()
    {
        var result = await IssueService.UpdateSolutionAsync(IssueDto.Id.Value, IssueDto);
        //todo:弹出提示
        if (result.IsSuccessStatusCode)
        {
            await JsRuntime.InvokeVoidAsync("alert", "保存措施成功！");
        }
        StateHasChanged();
    }
    private async void CloseIssue()
    {
        IssueDto.CloseTime=DateTime.Now;
        IssueDto.IsClosed = true;
        //记录关闭Issue的人
        //获取当前登录的用户
        var responder = (await AuthProvider.GetAuthenticationStateAsync()).User;
        var userId = responder.FindFirst(x => x.Type.Contains("nameidentifier"))?.Value;
        if (string.IsNullOrEmpty(userId))
        {
            NavigationManager.NavigateTo("login");
        }
        else
        {
            IssueDto.ResponderId = Guid.Parse(userId);
            var uResult = await UserService.GetFirstOrDefaultAsync(IssueDto.ResponderId.Value);
            IssueDto.Responder = uResult.Result.UserName;
            var result = await IssueService.UpdateSolutionAsync(IssueDto.Id.Value, IssueDto);
            //todo:弹出提示
            if (result.IsSuccessStatusCode)
            {
                await JsRuntime.InvokeVoidAsync("alert", "关闭异常成功！");
            }
            StateHasChanged();
        }
    }

}
